﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing;

namespace SuperSnake
{
    class Snake
    {
        public ArrayList snake = new ArrayList();

        public Snake() //构造函数，默认蛇从左上角出现
        {
            for (int i = GameField.snakeLength - 1; i >= 0; i--)
            {
                snakeblock b = new snakeblock(new Point(GameField.blockWidth * i, 0));
                snake.Add(b);
            }
        }

        /*移动*/
        public bool up()
        {
            snakeblock head = (snakeblock)snake[0];
            //有食物，吃掉
            if (GameField.haveFood(head.location.X, head.location.Y - GameField.blockWidth))
            {
                head = new snakeblock(GameField.food.location);
                snake.Insert(0, head);
                GameField.foodVisible = false;
                GameField.food.Erase(GameField.winHandle);
                GameField.createFood(this);
                GameField.foodVisible = true;
                return true;
            }
            //没有食物，正常移动
            if (GameField.isEmpty(head.location.X , head.location.Y  - GameField.blockWidth,this))
            {
                Erase(GameField.winHandle);
                snakeblock tail = (snakeblock)snake[snake.Count - 1];
                tail.Erase(GameField.winHandle);
                tail.location.Y = head.location.Y - GameField.blockWidth;
                tail.location.X = head.location.X;
                tail.Draw(GameField.winHandle);
                snake.RemoveAt(snake.Count - 1);
                snake.Insert(0, tail);
                Draw(GameField.winHandle);
                return true;
            }
            else
                return false;
        }
        public bool down()
        {
            snakeblock head = (snakeblock)snake[0];
            //有食物，吃掉
            if (GameField.haveFood(head.location.X, head.location.Y + GameField.blockWidth))
            {
                head = new snakeblock(GameField.food.location);
                snake.Insert(0, head);
                GameField.foodVisible = false;
                GameField.food.Erase(GameField.winHandle);
                GameField.createFood(this);
                GameField.foodVisible = true;
                return true;
            }
            //没有食物，正常移动
            if (GameField.isEmpty(head.location.X, head.location.Y + GameField.blockWidth,this))
            {
                Erase(GameField.winHandle);
                snakeblock tail = (snakeblock)snake[snake.Count - 1];
                tail.Erase(GameField.winHandle);
                tail.location.Y = head.location.Y + GameField.blockWidth;
                tail.location.X = head.location.X;
                tail.Draw(GameField.winHandle);
                snake.RemoveAt(snake.Count - 1);
                snake.Insert(0, tail);
                Draw(GameField.winHandle);
                return true;
            }
            else
                return false;
        }
        public bool left()
        {
            snakeblock head = (snakeblock)snake[0];
            //有食物，吃掉
            if (GameField.haveFood(head.location.X - GameField.blockWidth, head.location.Y))
            {
                head = new snakeblock(GameField.food.location);
                snake.Insert(0, head);
                GameField.foodVisible = false;
                GameField.food.Erase(GameField.winHandle);
                GameField.createFood(this);
                GameField.foodVisible = true;
                return true;
            }
            //没有食物，正常移动
            if (GameField.isEmpty(head.location.X - GameField.blockWidth, head.location.Y,this))
            {
                Erase(GameField.winHandle);
                snakeblock tail = (snakeblock)snake[snake.Count - 1];
                tail.Erase(GameField.winHandle);
                tail.location.Y = head.location.Y;
                tail.location.X = head.location.X - GameField.blockWidth;
                tail.Draw(GameField.winHandle);
                snake.RemoveAt(snake.Count - 1);
                snake.Insert(0, tail);
                Draw(GameField.winHandle);
                return true;
            }
            else
                return false;
        }
        public bool right()
        {
            snakeblock head = (snakeblock)snake[0];
            //有食物，吃掉
            if (GameField.haveFood(head.location.X + GameField.blockWidth, head.location.Y))
            {
                head = new snakeblock(GameField.food.location);
                snake.Insert(0, head);
                GameField.foodVisible = false;
                GameField.food.Erase(GameField.winHandle);
                GameField.createFood(this);
                GameField.foodVisible = true;
                return true;
            }
            //没有食物，正常移动
            if (GameField.isEmpty(head.location.X + GameField.blockWidth, head.location.Y,this))
            {
                Erase(GameField.winHandle);
                snakeblock tail = (snakeblock)snake[snake.Count - 1];
                tail.Erase(GameField.winHandle);
                tail.location.Y = head.location.Y;
                tail.location.X = head.location.X + GameField.blockWidth;
                tail.Draw(GameField.winHandle);
                snake.RemoveAt(snake.Count - 1);
                snake.Insert(0, tail);
                Draw(GameField.winHandle);
                return true;
            }
            else
                return false;
        }

        /*画蛇*/
        public void Draw(IntPtr winHandle)
        {
            foreach (snakeblock b in snake)
                b.Draw(winHandle);
        }
        /*擦蛇*/
        public void Erase(IntPtr winHandle)
        {
            foreach (snakeblock b in snake)
                b.Erase(winHandle);
        }

    }
}
